package com.gitee.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.bo.UserBo;
import com.gitee.mapper.UserMapper;
import com.gitee.pojo.Order;
import com.gitee.pojo.User;
import com.gitee.service.ICommentService;
import com.gitee.service.IOrderItemService;
import com.gitee.service.IOrderService;
import com.gitee.service.IUserService;
import com.gitee.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IOrderItemService orderItemService;
    /**
     * 查询用户
     *
     * @param keywords
     * @param enabled
     * @param admin
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVo<User> pageUser(String keywords, Boolean enabled, Boolean admin, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<User> queryWrapper = new QueryWrapper<User>().lambda()
                .like(!StrUtil.isBlank(keywords), User::getNickName, keywords)
                .eq(enabled != null, User::getEnabled, enabled)
                .eq(admin != null, User::getAdmin, admin);
        Page<User> page = super.page(new Page<>(pageNum, pageSize), queryWrapper);
        int total = (int)page.getTotal();
        return new PageVo<>(page.getRecords(), total);
    }

    /**
     * 删除用户及其关联订单
     *
     * @param userId
     * @return
     */
    @Transactional
    @Override
    public Boolean deleteUser(Integer userId) {
        List<Order> orders = orderService.listByUserId(userId);
        if (!CollectionUtil.isEmpty(orders)) {
            List<Integer> orderIds = orders.stream().map(Order::getId).collect(Collectors.toList());
            orderItemService.deleteByOrderIds(orderIds);
            commentService.deleteByOrderIds(orderIds);
        }
        return super.removeById(userId);
    }

    /**
     * 批量删除用户
     *
     * @param userIds
     * @return
     */
    @Transactional
    @Override
    public Boolean deleteUsers(Integer[] userIds) {
        List<Order> orders = orderService.listByUserIds(userIds);
        if (!CollectionUtil.isEmpty(orders)) {
            List<Integer> orderIds = orders.stream().map(Order::getId).collect(Collectors.toList());
            orderItemService.deleteByOrderIds(orderIds);
            commentService.deleteByOrderIds(orderIds);
        }
        return super.removeByIds(Arrays.asList(userIds));
    }

    /**
     * 修改用户权限
     * 是否可用
     * 是否小程序管理员
     * @param userId
     * @param userBo
     * @return
     */
    @Transactional
    @Override
    public Boolean updateUser(Integer userId, UserBo userBo) {
        LambdaUpdateWrapper<User> updateWrapper = new UpdateWrapper<User>().lambda()
                .set(userBo.getAdmin() != null, User::getAdmin, userBo.getAdmin())
                .set(userBo.getEnabled() != null, User::getEnabled, userBo.getEnabled())
                .eq(User::getId, userId);
        return super.update(updateWrapper);
    }
}
